package com.intelligence.pda.ui.scanlibrary.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.intelligence.pda.R;
import com.intelligence.pda.ui.scanlibrary.model.OutstockDetailModel;
import com.intelligence.pda.utils.AppConstants;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator
 * on 2017/10/19.
 */

public class OutstockDetailAdapter extends BaseAdapter {
    private Context mActivity;
    private LayoutInflater mInflater = null;

    public OutstockDetailAdapter(Activity activity) {
        this.mActivity = (Context) activity;
        this.mInflater = LayoutInflater.from(activity);
    }

    private List<OutstockDetailModel.DataBean> refundModels = new ArrayList<>();


    public void addData(List<OutstockDetailModel.DataBean> refundModels) {
        this.refundModels = refundModels;
        notifyDataSetChanged();
    }

    public void clearData() {
        this.refundModels.clear();
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return refundModels.size();
    }

    @Override
    public Object getItem(int position) {
        return refundModels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        OutstockDetailModel.DataBean prodectionModel = refundModels.get(position);
        ViewHolder holder = null;
        if (AppConstants.FILTER && prodectionModel.isOut()) {
            holder = new ViewHolder();
                //根据自定义的Item布局加载布局
                convertView = mInflater.inflate(R.layout.item_outstock_detail2, parent, false);

        } else {
            //如果缓存convertView为空，则需要创建View
            holder = new ViewHolder();
                //根据自定义的Item布局加载布局
                convertView = mInflater.inflate(R.layout.item_outstock_detail, parent, false);
                holder.tv_orderNo = (TextView) convertView.findViewById(R.id.tv_orderNo);
                holder.tv_extnorderNo = (TextView) convertView.findViewById(R.id.tv_extnorderNo);
                holder.tv_goodsName = (TextView) convertView.findViewById(R.id.tv_goodsName);
                holder.et_Nombre = (TextView) convertView.findViewById(R.id.et_Nombre);
                holder.tv_no_scan = (TextView) convertView.findViewById(R.id.tv_no_scan);
                holder.tv_rapport_Anormal = (TextView) convertView.findViewById(R.id.tv_rapport_Anormal);
                //将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag
                convertView.setTag(holder);


            holder.tv_orderNo.setText(prodectionModel.getOrderNo());
            holder.tv_extnorderNo.setText(prodectionModel.getExtnOrderNo());
            holder.tv_goodsName.setText(prodectionModel.getGoodsName());

            if(prodectionModel.isButton()){
                holder.tv_rapport_Anormal.setVisibility(View.VISIBLE);
            }else {
                holder.tv_rapport_Anormal.setVisibility(View.GONE);


            }

            if (!TextUtils.isEmpty(prodectionModel.getAmounts())) {
                holder.et_Nombre.setText(prodectionModel.getAmounts());
                if (prodectionModel.getAmounts().equals(prodectionModel.getAmount())) {
                    holder.et_Nombre.setTextColor(mActivity.getResources().getColor(R.color.textDarkGrey));
                } else {
                    holder.et_Nombre.setTextColor(mActivity.getResources().getColor(R.color.red));
                }

            }

            if (prodectionModel.isOut()) {
                holder.tv_no_scan.setText("已扫描");

                holder.tv_no_scan.setTextColor(mActivity.getResources().getColor(R.color.yes_scan));
            } else {
                holder.tv_no_scan.setText("未扫描");
                holder.tv_no_scan.setTextColor(mActivity.getResources().getColor(R.color.red));
            }
        }

        return convertView;


    }

    class ViewHolder {
        TextView tv_orderNo;
        TextView tv_extnorderNo;
        TextView tv_goodsName;
        TextView et_Nombre;
        TextView tv_no_scan;
        TextView tv_rapport_Anormal;
    }

}